#usr/bin/python
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from matplotlib import patches
from matplotlib import backend_bases as bb
from pylab import *

class modified_toolbar(NavigationToolbar):
	
	def __init__(self,  canvas,  parent,  coordinates= True):
		NavigationToolbar.__init__(self,  canvas,  parent)
		#self.canvas.renderer= bb.Renderer()
		self.rect = []
		#self.stack_rect=[]
	
	def draw_rubberband(self, event,  x0,  y0,  x1,  y1):
		self.stack_rect= []
		height = self.canvas.figure.bbox.height
		y1 = height - y1
		y0 = height - y0

		w = abs(x1 - x0)
		h = abs(y1 - y0)
		#self.rect =patches.Rectangle((int(min(x0,  x1)), int(0)), int(w), int(height),  fc='blue', ec='black', ls='solid',  alpha=0.009)
		#Xmin,  Xmax =self.canvas.get_xlim()
		#Ymin, Ymax =self.canvas.get_ylim()
		self.rect= [int(min(x0,  x1)), int(0), int(w), int(height)] 
		self.canvas.drawRectangle(self.rect)
	
	def release_zoom(self, event):
		'the release mouse button callback in zoom to rect mode'
		if not self._xypress: return

		last_a = []

		for cur_xypress in self._xypress:
			x, y = event.x, event.y
			lastx, lasty, a, ind, lim, trans = cur_xypress

			x0, y0, x1, y1 = lim.extents

			# zoom to rect
			inverse = a.transData.inverted()
			lastx, lasty = inverse.transform_point( (lastx, lasty) )
			x, y = inverse.transform_point( (x, y) )
			Xmin,Xmax=a.get_xlim()
			Ymin,Ymax=a.get_ylim()
			
			 # detect twinx,y axes and avoid double zooming
			twinx, twiny = False, False
			if last_a:
				for la in last_a:
					if a.get_shared_x_axes().joined(a,la): twinx=True
					if a.get_shared_y_axes().joined(a,la): twiny=True
			last_a.append(a)

			if twinx:
				x0, x1 = Xmin, Xmax
			else:
				if Xmin < Xmax:
					if x<lastx:  x0, x1 = x, lastx
					else: x0, x1 = lastx, x
					if x0 < Xmin: x0=Xmin
					if x1 > Xmax: x1=Xmax
				else:
					if x>lastx:  x0, x1 = x, lastx
					else: x0, x1 = lastx, x
					if x0 > Xmin: x0=Xmin
					if x1 < Xmax: x1=Xmax

			if twiny:
				y0, y1 = Ymin, Ymax
			else:
				if Ymin < Ymax:
					if y<lasty:  y0, y1 = y, lasty
					else: y0, y1 = lasty, y
					if y0 < Ymin: y0=Ymin
					if y1 > Ymax: y1=Ymax
				else:
					if y>lasty:  y0, y1 = y, lasty
					else: y0, y1 = lasty, y
					if y0 > Ymin: y0=Ymin
					if y1 < Ymax: y1=Ymax

			if self._button_pressed == 1:
				a.set_xlim((x0, x1))
			elif self._button_pressed == 3:
				if a.get_xscale()=='log':
					alpha=np.log(Xmax/Xmin)/np.log(x1/x0)
					rx1=pow(Xmin/x0,alpha)*Xmin
					rx2=pow(Xmax/x0,alpha)*Xmin
				else:
					alpha=(Xmax-Xmin)/(x1-x0)
					rx1=alpha*(Xmin-x0)+Xmin
					rx2=alpha*(Xmax-x0)+Xmin
				if a.get_yscale()=='log':
					alpha=np.log(Ymax/Ymin)/np.log(y1/y0)
					ry1=pow(Ymin/y0,alpha)*Ymin
					ry2=pow(Ymax/y0,alpha)*Ymin
				else:
					alpha=(Ymax-Ymin)/(y1-y0)
					ry1=alpha*(Ymin-y0)+Ymin
					ry2=alpha*(Ymax-y0)+Ymin
				a.set_xlim((rx1, rx2))

		self.draw()
		self._xypress = None
		self._button_pressed = None

		self.push_current()
		self.release(event)
		

if __name__=="__main__":
	from PyQt4 import QtGui
	import canvas
	app = QtGui.QApplication(sys.argv)

	x= [[1,4,10,3,5]]; y=[[4,3,9,2,3]]
	can = canvas.canvasUi(x, y)
	can.show()
	app.exec_()
	
